<!--创建学生成绩-->
<template>
  <el-container>
    <el-header>
      <h1>创建学生成绩</h1>
    </el-header>
    <el-main>
      <el-form ref="form" :model="user" :rules="rules" label-width="100px">
        <el-form-item label="学生姓名:" prop="username">
          <el-select v-model="user.username" placeholder="姓名">
            <el-option
                v-for="item in name"
                :value="item.username"
                v-model="user.email"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="科目:" prop="results">
          <el-select v-model="user.subjects" placeholder="科目">
            <el-option
                v-for="item in subjects"
                :key="item.grade"
                :value="item.grade"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="成绩:" prop="subjects">
          <el-input v-model="user.results"></el-input>
        </el-form-item>
      </el-form>
      <el-button @click="button">确认</el-button>
    </el-main>
  </el-container>
</template>

<script>
import Rules from "@/components/Rules";
import {request} from "@/network/request";
import {ElMessageBox} from "element-plus";

export default {
  name: "createResults",
  data() {
    return {
      subjects: null,
      name: null,
      user: {
        username: null,
        subjects: null,
        results: null,
        grade: this.$store.state.grade
      },
      rules: {
        // 用户名
        username: Rules.rules.username,
        //成绩
        results: Rules.rules.results,
        //科目
        subjects: Rules.rules.subjects,
      }
    }
  },
  mounted() {
    this.demo();
    this.demo2()
  },
  methods: {
    demo() {
      // 获得科目
      request({
        url: '/login/getgrade'
      }).then(res => {
        this.$data.subjects = res.data.data;
      }).catch(err => {
        this.$message({
          type: 'warning',
          message: '输出错误' + err
        });
      })

    },
    demo2() {
      // 获取姓名
      request({
        url: '/login/showclassstudent',
        params: {
          grade: this.$store.state.grade
        }
      }).then(res => {
        this.$data.name = res.data.data;
      }).catch(err => {
        this.$message({
          type: 'warning',
          message: '输出错误' + err
        });
      })
    },
    button() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //判断是否有成绩
          request({
            url: '/login/showresults',
            params: {
              username: this.$data.user.username,
              grade: this.$data.user.grade,
              subjects: this.$data.user.subjects
            }
          }).then(res => {
            if (res.data.success) {
              // 无数据
              request({
                url: '/login/Resutls',
                params: {
                  username: this.$data.user.username,
                  grade: this.$data.user.grade,
                  subjects: this.$data.user.subjects,
                  results: this.$data.user.results,
                }
              }).then(res => {
                if (res.data.success) {
                  this.$message({
                    type: 'success',
                    message: res.data.message
                  });
                  this.$data.user.username = null;
                  this.$data.user.subjects = null;
                  this.$data.user.results = null;
                } else {
                  this.$message({
                    type: 'warning',
                    message: res.data.message
                  });
                }
              }).catch(err => {
                this.$message({
                  type: 'warning',
                  message: '数据错误' + err
                });
              })
            } else {
              ElMessageBox.confirm(
                  '有相同的数据将会覆盖上一个数据，你确定要添加？',
                  'Warning',
                  {
                    confirmButtonText: '是',
                    cancelButtonText: '否',
                    type: 'warning',
                  }
              ).then(() => {
                // 删除上一条数据
                request({
                  url: '/login/delectresults',
                  params: {
                    username: this.$data.user.username,
                    grade: this.$data.user.grade,
                    subjects: this.$data.user.subjects,
                  }
                }).then(res => {
                  if (res.data.success) {
                    // 新增数据
                    request({
                      url: '/login/Resutls',
                      params: {
                        username: this.$data.user.username,
                        subjects: this.$data.user.subjects,
                        grade: this.$data.user.grade,
                        results: this.$data.user.results,
                      }
                    }).then(res => {
                      if (res.data.success) {
                        this.$message({
                          type: 'success',
                          message: res.data.message
                        });
                        this.$data.user.username = null;
                        this.$data.user.subjects = null;
                        this.$data.user.results = null;
                      } else {
                        this.$message({
                          type: 'warning',
                          message: res.data.message
                        });
                      }
                    }).catch(err => {
                      this.$message({
                        type: 'warning',
                        message: '数据错误' + err
                      });
                    })
                  } else {
                    this.$message({
                      type: 'warning',
                      message: res.data.message
                    });
                  }
                }).catch(err => {
                  this.$message({
                    type: 'warning',
                    message: '数据错误' + err
                  });
                })
              }).catch(() => {
                this.$data.user.username = null;
                this.$data.user.subjects = null;
                this.$data.user.results = null;
              })
            }
          }).catch(err => {
            this.$message({
              type: 'warning',
              message: '数据错误' + err
            });
          })
        } else {
          this.$message({
            type: 'warning',
            message: '校验失败'
          });
        }
      })
    }
  }
}
</script>

<style scoped>

</style>